Socket
Socket
Sign inDemoInstall

acorn-walk

Package Overview
Dependencies
Maintainers
3
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

acorn-walk

ECMAScript (ESTree) AST walker


Version published
Weekly downloads
44M
decreased by-0.42%
Maintainers
3
Weekly downloads
 
Created

What is acorn-walk?

The acorn-walk package is a syntax tree walker for the abstract syntax trees (ASTs) that Acorn produces. It allows developers to easily traverse and manipulate JavaScript code structures represented as ASTs. This is useful for tasks such as static analysis, code transformation, and optimization.

What are acorn-walk's main functionalities?

Simple tree walking

This feature allows you to perform a simple walk over the AST nodes and apply specific functions to nodes of a particular type. In the code sample, `walk.simple` is used to log all literals in the parsed code.

const acorn = require('acorn');
const walk = require('acorn-walk');
const ast = acorn.parse('let x = 10;', {ecmaVersion: 2020});
walk.simple(ast, {
  Literal(node) {
    console.log(`Found a literal: ${node.value}`);
  }
});

Full tree walking

This feature allows you to visit every node in the AST, regardless of its type. The code sample demonstrates using `walk.full` to log the type of every node in the AST.

const acorn = require('acorn');
const walk = require('acorn-walk');
const ast = acorn.parse('let x = 10;', {ecmaVersion: 2020});
walk.full(ast, node => {
  console.log(`Type of node: ${node.type}`);
});

Ancestor tree walking

This feature allows you to walk the AST like `walk.simple`, but it also provides an array of ancestor nodes to each callback. The code sample logs each literal found and its ancestors.

const acorn = require('acorn');
const walk = require('acorn-walk');
const ast = acorn.parse('let x = 10;', {ecmaVersion: 2020});
walk.ancestor(ast, {
  Literal(node, ancestors) {
    console.log(`Found a literal with value ${node.value} and ancestors ${ancestors.map(a => a.type).join(', ')}`);
  }
});

Other packages similar to acorn-walk

FAQs

Package last updated on 09 Sep 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc